package com.zhihuishu.micro.course.openapi.app.impl.teacher;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhihuishu.micro.course.log.LoggerCollectionEnum;
import com.zhihuishu.micro.course.log.LoggerTemplateUtil;
import com.zhihuishu.micro.course.model.CourseBuild;
import com.zhihuishu.micro.course.openapi.app.dto.CourseBuildOpenDto;
import com.zhihuishu.micro.course.openapi.app.enmus.CourseBuildStepEnum;
import com.zhihuishu.micro.course.openapi.app.teacher.CourseBuildOpenService;
import com.zhihuishu.micro.course.service.CourseBuildService;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.toolkit.helper.BeanHelper;
@Service("courseBuildOpenServiceForTeacherApp")
public class CourseBuildOpenServiceImpl implements CourseBuildOpenService {

    protected Logger logger=LoggerFactory.getLogger(getClass());
    @Resource
    private CourseBuildService courseBuildService;

    @Override
    public RemoteResult<Void> updateCourseBuildStats(long courseId, CourseBuildStepEnum buildStepEnum,
            int stepStatus,long userId) {
        RemoteResult<Void> remoteResult=new RemoteResult<Void>();
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("接收到更新课程状态的参数为,courseId={},step={},status={},userId={}",courseId,buildStepEnum.getStep(),stepStatus,userId);
            }
            courseBuildService.updateCourseStepStat(courseId,buildStepEnum.getStep(),stepStatus);
            LoggerTemplateUtil.saveLogger(String.format("更新课程%d,步骤%d,完整度状态为%d",courseId,buildStepEnum.getStep(),stepStatus), userId, LoggerCollectionEnum.courseBuild, courseId);
        } catch (Exception e) {
            remoteResult.setExceptionStack(e);
            remoteResult.setSuccess(false);
        }
        return remoteResult;
       
    }

    @Override
    public RemoteResult<CourseBuildOpenDto> queryCourseBuildStatById(long courseId) {
        RemoteResult<CourseBuildOpenDto> remoteResult=new RemoteResult<CourseBuildOpenDto>();
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("根据课程id查询完整度状态,courseId={}",courseId);
            }
            CourseBuild courseBuild=courseBuildService.queryCourseBuildStatById(courseId);
            if (logger.isDebugEnabled()) {
                logger.debug("查询到课程完整度状态信息为{}",courseBuild);
            }
            CourseBuildOpenDto courseBuildOpenDto=BeanHelper.transTo(courseBuild, CourseBuildOpenDto.class);
            if (logger.isDebugEnabled()) {
                logger.debug("转换后课程完整度状态信息为courseBuildOpenDto={}",courseBuildOpenDto);
            }
            remoteResult.setResult(courseBuildOpenDto);;
        } catch (Exception e) {
            remoteResult.setExceptionStack(e);
            remoteResult.setSuccess(false);
            logger.error("查询课程完整度出错,courseId={}",courseId,e);
            
        }
        return remoteResult;

    }
    
    
}
